#include "VSCamera.h"
#include "..\glm\gtx\transform.hpp"


VSCamera::VSCamera(void)
{
	ResetCamera();
}


VSCamera::~VSCamera(void)
{
}

void VSCamera::ResetCamera()
{
	m_distance = 10;
	setViewpoint(Point3D(0,0,0));
	m_hangle = 0;
	m_vangle = 0;
}

void VSCamera::RotateCamera( float angle, RotatePlane rotatePlane )
{
	if(rotatePlane == H)
		m_hangle -= angle;
	else if(rotatePlane == V)
		m_vangle -= angle;
}

void VSCamera::MoveCamera( Point3D direction )
{
	m_viewpoint += Point3D((glm::inverse (glm::rotate (-m_vangle, glm::vec3 (1, 0, 0)) * 
		glm::rotate (-m_hangle, glm::vec3 (0, 1, 0))) * glm::vec4 (direction.x,direction.y,direction.z, 0.0)).
		swizzle (glm::X, glm::Y, glm::Z));
}

void VSCamera::setViewpoint( const Point3D & loc )
{
	m_viewpoint = loc;
}

void VSCamera::setDistance( float distance )
{
	m_distance = distance;
}

void VSCamera::Render( void )
{
	m_transformMat = 
		glm::translate< float > (0, 0, -m_distance) *
		glm::rotate (-m_vangle, glm::vec3 (1, 0, 0)) * 
		glm::rotate (-m_hangle, glm::vec3 (0, 1, 0)) *
		glm::translate< float > (-m_viewpoint.x,
		-m_viewpoint.y, -m_viewpoint.z);
	glPushAttrib (GL_MATRIX_MODE);
		glMatrixMode (GL_MODELVIEW);
		glLoadMatrixf (&m_transformMat[0][0]);
	glPopAttrib ();
}

void VSCamera::ScaleCamera( float factor )
{
	m_distance -= factor;
}

void VSCamera::ScaleCamera( Point3D factor )
{
	m_distance += factor.length();
}
